package com.blog.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.admin.listener.event.CacheEvent;
import com.blog.admin.service.FileInfoService;
import com.blog.admin.service.PhotoService;
import com.blog.common.core.enums.FileEnums;
import com.blog.common.core.web.page.TableDataInfo;
import com.blog.common.domain.dto.PhotoDto;
import com.blog.common.domain.dto.PhotoUploadDto;
import com.blog.common.domain.entity.FileInfo;
import com.blog.common.domain.entity.Photo;
import com.blog.common.domain.vo.PhotoBackVo;
import com.blog.common.mapper.dao.PhotoMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.blog.common.core.constant.CacheConstants.FILE_INFO;

/**
 * <p>
 * 照片 服务实现类
 * </p>
 *
 * @author Valerie
 * @since 2023-03-11
 */
@Service
@RequiredArgsConstructor
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {

    private final PhotoMapper photoMapper;

    private final FileInfoService fileInfoService;

    private final ApplicationEventPublisher eventPublisher;


    @Override
    public TableDataInfo<PhotoBackVo> listPhotoByAlbumId(Serializable albumId) {
        List<Photo> photoList = photoMapper.selectList(new LambdaQueryWrapper<Photo>()
                .eq(Photo::getAlbumId, albumId)
                .orderByDesc(Photo::getId)
        );
        return new TableDataInfo<>(BeanUtil.copyToList(photoList, PhotoBackVo.class), photoList.size());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePhotoList(PhotoUploadDto photoUploadDto) {
        List<String> photoUrlList = photoUploadDto.getPhotoUrlList();
        List<FileInfo> fileInfoList = photoUrlList.stream()
                .map(item -> FileInfo.builder()
                        .id(IdUtil.getSnowflakeNextId())
                        .filePath(item)
                        .fileName("相册图片")
                        .fileType(FileEnums.ALBUM_FILE.getCode())
                        .build())
                .collect(Collectors.toList());
        Map<String, Long> fileInfoMap = fileInfoList.stream().collect(Collectors.toMap(FileInfo::getFilePath, FileInfo::getId));
        List<Photo> photoList = photoUrlList.stream()
                .map(item -> Photo.builder()
                        .albumId(photoUploadDto.getAlbumId())
                        .photoSrc(fileInfoMap.get(item).toString())
                        .build()
                ).collect(Collectors.toList());
        fileInfoService.saveBatch(fileInfoList);
        PhotoService photoService = SpringUtil.getBean(this.getClass());
        photoService.saveBatch(photoList);
        eventPublisher.publishEvent(new CacheEvent(this, FILE_INFO));
    }

    @Override
    public Boolean updatePhoto(PhotoDto photoDto) {
        return new LambdaUpdateChainWrapper<>(photoMapper)
                .eq(Photo::getId, photoDto.getId())
                .set(StringUtils.isNotBlank(photoDto.getPhotoName()), Photo::getPhotoName, photoDto.getPhotoName())
                .set(StringUtils.isNotBlank(photoDto.getPhotoDesc()), Photo::getPhotoDesc, photoDto.getPhotoDesc())
                .update();
    }
}
